package com.example.spring_config.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.spring_config.anno.PermissionVerAnno;
import com.example.spring_config.bean.LayuiTree;
import com.example.spring_config.bean.ResultInfo;
import com.example.spring_config.mapper.Role;
import com.example.spring_config.service.PermissionRoleRelationService;
import com.example.spring_config.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionRoleRelationService permissionRoleRelationService;

    /**
     * 获取角色列表
     * @return 结果对象
     */
    @PermissionVerAnno("getRoleList")
    @RequestMapping("/getRoleList")
    public ResultInfo getRoleList(Page<Role> page){
        page = roleService.page(page);
        if(page.getRecords().size()<=0){
            // 没获取到数据
            return ResultInfo.no("没有角色列表");
        }else {
            return ResultInfo.ok(page);
        }
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @PermissionVerAnno("addRole")
    @RequestMapping("/addRole")
    public ResultInfo addRole(Role role){
        boolean save = roleService.save(role);
        if(save){
            return ResultInfo.ok();
        }else {
            return ResultInfo.no("添加角色失败");
        }
    }

    /**
     * 根据id删除角色
     * @param id 角色id
     * @return 结果对象
     */
    @PermissionVerAnno("deleteRoleById")
    @RequestMapping("/deleteRoleById")
    public ResultInfo deleteRoleById(String id){
        boolean b = roleService.removeById(id);
        if(b){
            return ResultInfo.ok();
        }else {
            return ResultInfo.no("角色删除失败");
        }
    }

    /**
     * 根据角色id修改角色
     * @param role
     * @return
     */
    @PermissionVerAnno("updateRoleById")
    @RequestMapping("/updateRoleById")
    public ResultInfo updateRoleById(Role role){
        boolean b = roleService.updateById(role);
        if(b){
            return ResultInfo.ok();
        }else {
            return ResultInfo.no("角色修改失败");
        }
    }

    /**
     * 根据角色id查询角色
     * @param id 角色id
     * @return 结果对象
     */
    @PermissionVerAnno("selectRoleById")
    @RequestMapping("/selectRoleById")
    public ResultInfo selectRoleById(String id){
        Role byId = roleService.getById(id);
        if(byId==null){
            return ResultInfo.no("未找到此角色");
        }else {
          return ResultInfo.ok(byId);
        }
    }

    /**
     * 给角色赋予权限
     * @param rid 角色id
     * @param pids 权限集合
     * @return 结果对象
     */
    @PermissionVerAnno("addRolePermission")
    @RequestMapping("/addRolePermission")
    public ResultInfo addRolePermission(@RequestParam("rid") String rid,@RequestParam("pids") List<String> pids){
        Boolean aBoolean = permissionRoleRelationService.addRolePermission(rid, pids);
        if(aBoolean){
            return ResultInfo.ok();
        }else {
            return ResultInfo.no("权限分配失败");
        }
    }

    /**
     * 根据用户的id获取所有角色
     * @param uid 用户id
     * @return 结果对象
     */
    @PermissionVerAnno("getRoleListAndUserRole")
    @RequestMapping("/getRoleListAndUserRole")
    public Object getRoleList(String uid){
        List<LayuiTree> roleByUser = roleService.getRoleByUser(uid);
        if(roleByUser==null || roleByUser.size()==0){
            return ResultInfo.no("角色获取失败");
        }else {
            return roleByUser;
        }
    }

}
